package com.carlinfo.userscenter.users.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.carlinfo.userscenter.common.dao.impl.BaseDaoImpl;
import com.carlinfo.userscenter.common.util.PageInfoUtil;
import com.carlinfo.userscenter.users.dao.IAAdminsDao;
import com.carlinfo.userscenter.users.pojo.AAdmins;


public class AAdminsDaoImpl extends BaseDaoImpl<AAdmins> implements IAAdminsDao
{

	@Override
	public int saveOne(AAdmins admins)
	{
		System.out.println("==AAdminsDaoImpl==saveOne==");
		
		String sql ="insert into `a_admins`(`roleId`, `email`, `password`, `photoPath`, `trueName`, "
				+ "`phone`, `qq`, `loginFailedCount`, `loginFailedTime`, `loginCount`, `lastLoginIp`, "
				+ "`status`, `createTime`, `updateTime`, `pubTime`, `lastloginTime`) "
				+ "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		
		List<Object> paramsList = new ArrayList<Object>();

		paramsList.add(admins.getRoleId());
		paramsList.add(admins.getEmail());
		paramsList.add(admins.getPassword());
		paramsList.add(admins.getPhotoPath());
		paramsList.add(admins.getTrueName());
		paramsList.add(admins.getPhone());
		paramsList.add(admins.getQq());
		paramsList.add(admins.getLoginFailedCount());
		paramsList.add(admins.getLoginFailedTime());
		paramsList.add(admins.getLoginCount());
		paramsList.add(admins.getLastLoginIp());
		paramsList.add(admins.getStatus());
		paramsList.add(admins.getCreateTime());
		paramsList.add(admins.getUpdateTime());
		paramsList.add(admins.getPubTime());
		paramsList.add(admins.getLastloginTime());
		
		int res = this.executeSqlUpdate(sql, paramsList);
		admins.setId((int) paramsList.get(paramsList.size() - 1));
		return res ;

	}

	@Override
	public int updateOne(AAdmins admins)
	{
		String sql ="update `a_admins` set `roleId`  =? , `email` =? , `password` =? , `photoPath` =? , `trueName` =? , `phone` =? , `qq` =? , `loginFailedCount` =? , `loginFailedTime` =? , "
				+ "`loginCount` =? , `lastLoginIp` =? , `status` =? , "
				+ "`createTime` =? , `updateTime` =? , `pubTime` =? , `lastloginTime` = ?"
				+ " where `id` = ?" ;
		
		List<Object> paramsList = new ArrayList<Object>();

		paramsList.add(admins.getRoleId());
		paramsList.add(admins.getEmail());
		paramsList.add(admins.getPassword());
		paramsList.add(admins.getPhotoPath());
		paramsList.add(admins.getTrueName());
		paramsList.add(admins.getPhone());
		paramsList.add(admins.getQq());
		paramsList.add(admins.getLoginFailedCount());
		paramsList.add(admins.getLoginFailedTime());
		paramsList.add(admins.getLoginCount());
		paramsList.add(admins.getLastLoginIp());
		paramsList.add(admins.getStatus());
		paramsList.add(admins.getCreateTime());
		paramsList.add(admins.getUpdateTime());
		paramsList.add(admins.getPubTime());
		paramsList.add(admins.getLastloginTime());
		paramsList.add(admins.getId());

		return this.executeSqlUpdate(sql, paramsList);
	}

	@Override
	public AAdmins findOne(Map<String, Object> condMap)
	{
		List<Object> paramsList = new ArrayList<Object>();
		
		String sql = "select * from a_admins ";
		
		if (condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sql += "where id = ?";
			paramsList.add(condMap.get("id"));
		} else if (condMap.get("email") != null && !"".equalsIgnoreCase(condMap.get("email") + ""))
		{
			sql += "where email = ?";
			paramsList.add(condMap.get("email"));
		}
		System.out.println("AAdmins==findOne==sql==" + sql);
		System.out.println("AAdmins==findOne==条件==" + paramsList);

		return this.findOneBase(sql,paramsList, new AAdmins());

}

	@Override
	public int deleteOne(AAdmins admins)
	{
		String sql ="delete from a_admins "+ " where `id` = ? " ; 
		List<Object> paramsList = new ArrayList<Object>();
		paramsList.add(admins.getId());
		return this.executeSqlUpdate(sql, paramsList);
	}
	@Override
	public List<AAdmins> findCondList(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		System.out.println("==AAdminsDaoImpl==findCondList==");
		
		List<Object> paramList = new ArrayList<Object>();
		
		String sql = "select * from a_admins where 1 = 1 ";
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			sql = sql + " and (name like ?  or phone like ? or content like ?)";
			paramList.add("%" + condMap.get("keyword") + "%");
			paramList.add("%" + condMap.get("keyword") + "%");
			paramList.add("%" + condMap.get("keyword") + "%");
		}
	
		if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sql = sql + " and status = ?";
			paramList.add(condMap.get("status"));
		}
	
		if(condMap.get("stTime") instanceof Date && condMap.get("edTime") instanceof Date)
		{
			sql = sql + " and createTime > ? and createTime < ? " ; 
			paramList.add(condMap.get("stTime"));
			paramList.add(condMap.get("edTime"));
		}
	
		if ("iddesc".equalsIgnoreCase(condMap.get("orderby") + ""))
		{
			sql = sql + "order by id desc ";
		} else if ("idasc".equalsIgnoreCase(condMap.get("orderby") + ""))
		{
			sql = sql + "order by id asc ";
		} else
		{
			sql = sql + "order by id desc ";
		}
		System.out.println("AAdmins==findOne==sql==" + sql);
		if (pageInfoUtil != null)
		{
			return this.findByPage(pageInfoUtil, sql, paramList, new AAdmins());
		}
		return this.find(sql, paramList, new AAdmins());
	}
}
